<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/12
 * Time: 13:34
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Order as orderModel;
use app\api\model\OrderDetails;
class Order extends Nocommon
{
    /**
     * 订单列表
     */
    public function order()
    {
        if(Request::instance()->isPost())
        {
            $post = Request::instance()->post();
            $type = input('type','1','trim');
            $order = new orderModel();
            $data = $order->myOrder($post['uid'],$type);
            $this->apiReturn('200','成功',$data);
        }
    }
    /**
     * 删除订单
     */
    public function del_order()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            if($post['type']==1){
                $res = Db::name('tour_order')->where('order_id',$post['order_id'])->setField('is_del',1);
                if($res){
                    $this->apiReturn('200','删除成功');
                }else{
                    $this->apiReturn('400','删除失败');
                }
            }

            if($post['type']==2){
                $res = Db::name('hotel_order')->where('order_id',$post['order_id'])->setField('is_del',1);
                if($res){
                    $this->apiReturn('200','删除成功');
                }else{
                    $this->apiReturn('400','删除失败');
                }
            }

            if($post['type']==3){
                $res = Db::name('gtour_order')->where('order_id',$post['order_id'])->setField('is_del',1);
                if($res){
                    $this->apiReturn('200','删除成功');
                }else{
                    $this->apiReturn('400','删除失败');
                }
            }

            if($post['type']==4){
                $res = Db::name('guide_order')->where('order_id',$post['order_id'])->setField('is_del',1);
                if($res){
                    $this->apiReturn('200','删除成功');
                }else{
                    $this->apiReturn('400','删除失败');
                }
            }
        }
    }
    /*
     *
     * 评价 页面
     */
    public function appraise()
    {
        if(Request::instance()->isPost())
        {
            $post  = Request::instance()->post();
            if($post['type']==1){
                //查询数据
                $res = Db::name('tour_order')->alias('a')->join('tours t','a.tour_id=t.tour_id','left')
                    ->field('a.order_id,a.tour_id,t.tour_name')
                    ->where('order_id',$post['order_id'])
                    ->find();
                $res['item_id'] = $res['tour_id'];
                $res['title'] = $res['tour_name'];
                $res['type'] = 1;
                unset($res['tour_id']);
                unset($res['tour_name']);
            }
            if($post['type']==2){
                $res = Db::name('hotel_order')->alias('a')->join('hotel h','a.hotel_id=h.hotel_id','left')
                    ->field('a.order_id,a.hotel_id,h.hotel_name')
                    ->where('order_id',$post['order_id'])
                    ->find();
                $res['item_id'] = $res['hotel_id'];
                $res['title'] = $res['hotel_name'];
                $res['type'] = 2;
                unset($res['hotel_id']);
                unset($res['hotel_name']);
            }
            if($post['type']==3){
                $res = Db::name('gtour_order')->alias('a')->join('group_tour gt','a.gtour_id=gt.id','left')
                    ->field('a.order_id,a.gtour_id,gt.gtour_title')
                    ->where('order_id',$post['order_id'])
                    ->find();
                $res['item_id'] = $res['gtour_id'];
                $res['title'] = $res['gtour_title'];
                $res['type'] = 3;
                unset($res['gtour_id']);
                unset($res['gtour_title']);
            }
            if($post['type']==4){
                $res = Db::name('guide_order')->alias('a')->join('guide g','a.guide_id=g.guide_id','left')
                    ->field('a.order_id,a.guide_id,g.name')
                    ->where('order_id',$post['order_id'])
                    ->find();
                $res['item_id'] = $res['guide_id'];
                $res['title'] = $res['name'];
                $res['type'] = 4;
                unset($res['guide_id']);
                unset($res['name']);
            }
            $this->apiReturn('200','成功',$res);
        }
    }

    /*
     * 进行评价
     */
//    public function insert_app()
//    {
//        if(Request::instance()->isPost()){
//            $post = Request::instance()->post();
//            if($post['star']>=3){
//                $post['eval_status'] = 1;
//            }else{
//                $post['eval_status'] = 2;
//            }
//            $post['user_id'] = $post['uid'];
//            unset($post['uid']);
//            $post['create_time'] = time();
//
//            $res = Db::name('appraise')->insert($post);
//            if($res){
//                $this->apiReturn('200','添加成功');
//            }else{
//                $this->apiReturn('400','添加失败');
//            }
//        }
//    }

    public function insert()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            if($post['star']>=3){
                $data = [
                   'order_id'=>$post['order_id'],
                   'type'=>$post['type'],
                   'item_id'=>$post['item_id'],
                   'user_id'=>$post['uid'],
                   'content'=>$post['content'],
                   'images'=>$post['images'],
                   'eval_status'=>1,
                   'star'=>$post['star'],
                   'create_time'=>time(),
               ];
            }else{
                $data = [
                   'order_id'=>$post['order_id'],
                   'type'=>$post['type'],
                   'item_id'=>$post['item_id'],
                   'user_id'=>$post['uid'],
                   'content'=>$post['content'],
                   'images'=>$post['images'],
                   'eval_status'=>2,
                   'star'=>$post['star'],
                   'create_time'=>time(),
               ];
            }
          
            $res = Db::name('appraise')->insert($data);
            if($res){
                //修改订单状态
                if($post['type']==2){
                    $re = Db::name('hotel_order')->where('order_id',$post['order_id'])->setField('order_status',4);
                    if($re){
                        $this->apiReturn('200','评价成功');
                    }
                }
                if($post['type']==1){
                     $re = Db::name('tour_order')->where('order_id',$post['order_id'])->setField('order_status',4);
                     if($re){
                         $this->apiReturn('200','评价成功');
                     }
                }
                if($post['type']==3){
                    $re = Db::name('gtour_order')->where('order_id',$post['order_id'])->setField('order_status',4);
                    if($re){

                        Db::name('group_tour')->where('id',$post['item_id'])->setInc('app_sum',1);

                        $this->apiReturn('200','评价成功');
                    }
                }
               
            }else{
                $this->apiReturn('400','添加失败');
            }
        }
    }

    /**
     * 订单 详情
     */
    public function order_details()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $order = new OrderDetails();
            $data = $order->orderDetails($post['order_id'],$post['type']);
            $this->apiReturn('200','成功',$data);
        }
    }

    /*
     * 投诉订单
     */
    public function complaint()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
           
            $data = [
                'object_name'=>$post['object_name'],
                'object_id'=>$post['object_id'],
                'object_type'=>$post['object_type'],
                'order_sn'=>$post['order_sn'],
                'order_type'=>$post['type'],
                'u_id'=>$post['uid'],
                'complain_annex'=>$post['complain_annex'],
                'complain_content'=>$post['complain_content'],
                'create_time'=>time(),
            ];
            //查看投诉订单表是否有该订单投诉数据
            $info = Db::name('order_complains')
            ->where(['order_sn'=>$post['order_sn'],'object_type'=>$post['object_type']])
            ->find();
            if($info){
                $this->apiReturn('400','请勿重复投诉');
            }
            $res = Db::name('order_complains')->insert($data);
            if($res){
                $this->apiReturn('200','投诉成功');
            }else{
                $this->apiReturn('400','投诉失败');
            }
        }
    }

    /*
     * 完成服务
     */
    public function finish_fw()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //修改订单状态
            if($post['type']==1){
                $res = Db::name('tour_order')->where('order_id',$post['order_id'])->setField('order_status',4);
            }
            if($post['type']==2){
                $res = Db::name('hotel_order')->where('order_id',$post['order_id'])->setField('order_status',4);
            }
            if($post['type']==3){
                $res = Db::name('gtour_order')->where('order_id',$post['order_id'])->setField('order_status',4);
            }
            if($post['type']==4){
                $res = Db::name('guide_order')->where('order_id',$post['order_id'])->setField('order_status',4);
            }
            if($res){
                $this->apiReturn('200','完成服务成功');
            }else{
                $this->apiReturn('400','失败');
            }
        }
    }
    /*
     * 待支付状态的取消预订
     */
    public function d_cancel()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //修改订单状态
            if($post['type']==1){
                $res = Db::name('tour_order')->where('order_id',$post['order_id'])->setField('order_status',5);
                if($res){
                    $message = Db::name('tour_order')
                        ->where('order_id',$post['order_id'])->find();
                    if($message){
                        $data = [
                            'msg_type'=>2,
                            'receive_user_id'=>$post['uid'],
                            'msg_title'=>'订单取消预订提醒',
                            'is_read'=>0,
                            'create_time'=>date('Y-m-d'),
                            'msg_content'=>"您的订单{$message['order_sn']}，已取消预订，祝您生活愉快",
                            'oid'=>$post['order_id'],
                            'type'=>'2',
                        ];
                        $insert = Db::name('message')->insert($data);
                        if($insert){
                            $this->apiReturn('200','取消订单成功');
                        }else{
                            $this->apiReturn('400','取消订单失败');
                        }
                    }
                }
            }
            if($post['type']==2){
                $res = Db::name('hotel_order')->where('order_id',$post['order_id'])->setField('order_status',5);
                if($res){
                     $message = Db::name('hotel_order')
                    ->where('order_id',$post['order_id'])->find();
                    if($message){
                        $data = [
                            'msg_type'=>2,
                             'receive_user_id'=>$post['uid'],
                            'msg_title'=>'订单取消预订提醒',
                            'is_read'=>0,
                            'create_time'=>date('Y-m-d'),
                             'msg_content'=>"您的订单{$message['order_sn']}，已取消预订，祝您生活愉快",
                            'oid'=>$post['order_id'],
                            'type'=>'2',
                        ];
                        $insert = Db::name('message')->insert($data);
                         if($insert){
                         $this->apiReturn('200','取消订单成功');
                        }else{
                            $this->apiReturn('400','取消订单失败');
                        }
                    }
                }
            }
            if($post['type']==3){
                $res = Db::name('gtour_order')->where('order_id',$post['order_id'])->setField('order_status',5);
                 if($res){
                     $message = Db::name('gtour_order')
                    ->where('order_id',$post['order_id'])->find();
                    if($message){
                        $data = [
                            'msg_type'=>2,
                             'receive_user_id'=>$post['uid'],
                            'msg_title'=>'订单取消预订提醒',
                            'is_read'=>0,
                            'create_time'=>date('Y-m-d'),
                             'msg_content'=>"您的订单{$message['order_sn']}，已取消预订，祝您生活愉快",
                            'oid'=>$post['order_id'],
                            'type'=>'3',
                        ];
                        $insert = Db::name('message')->insert($data);
                         if($insert){
                         $this->apiReturn('200','取消订单成功');
                        }else{
                         $this->apiReturn('400','取消订单失败');
                        }
                    }
                }
            }
        }
    }
    
    /**
     * [cancel_order 待出行状态的取消订单]
     * @Author   kendy
     * @DateTime 2019-12-20T09:23:11+0800
     * @return   [type]                   [description]
     */
    public function cancelOrder()
    {
         if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $order = new orderModel(); 
            $order->cancelOrder();   
        } 
        
    }



}